package com.rainmonth.nicehair.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.rainmonth.nicehair.R;
import com.rainmonth.nicehair.common.CircleImageView;
import com.rainmonth.nicehair.common.model.Comment;
import com.rainmonth.nicehair.ui.activity.PostInfoActivity;
import com.rainmonth.nicehair.utils.ImageFetcher;
import com.rainmonth.nicehair.utils.Utils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PostAdapter extends BaseAdapter {

    private List<Comment> mListComment;
    private Context mContext;
    private ImageFetcher mImageFetcher;
    private int color;

    public PostAdapter(Context context, List<Comment> ListComment) {
        this.mContext = context;
        this.mListComment = ListComment;
        mImageFetcher = new ImageFetcher(mContext, 200);
        mImageFetcher.setLoadingImage(R.drawable.zone_list_profile);
        color = context.getResources().getColor(R.color.zone_list_username);
    }

    @Override
    public int getCount() {
        return mListComment == null ? 1 : mListComment.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.post_info_comment_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Comment com = mListComment.get(position);
        mImageFetcher.loadImage(com.getHeadUrl(), holder.mPostHeadIv);
        holder.mPostNameTv.setText(com.getName());
        holder.mPostTimeTv.setText(com.getTime());
        String content = com.getContent();
        holder.mPostContent.setText(content);
        Utils.addLinks(color, "", holder.mPostContent);
        holder.mPostChat.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PostInfoActivity.POST_RECEIVED_ACTION);
                intent.putExtra("uid", com.getUid());
                intent.putExtra("name", com.getName());
                mContext.sendBroadcast(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.post_info_head)
        CircleImageView mPostHeadIv;
        @Bind(R.id.post_info_username)
        TextView mPostNameTv;
        @Bind(R.id.post_addtime)
        TextView mPostTimeTv;
        @Bind(R.id.post_comment_huifu)
        TextView mPostChat;
        @Bind(R.id.post_content)
        TextView mPostContent;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
